<?php
/**
* This file belongs to a webappkit package (kit)
* @link http://webappkit.net
* @package databeans
* @subpackage userauthsession
* @author J.Ducastel <nospam0@ducastel.name>
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*/

/**
* user authenticated sessions storage
* @uses sqlRecordBean
* @uses dateTimeBean
*/
class userAuthSessionBean extends sqlRecordBean {
/*------------------------------------------------------------------------------
                                                                         members
------------------------------------------------------------------------------*/
    /**
    * @var type desc
    */
    var $id;
    /**
    * creates auto id
    * @access protected
    * @return string
    */
    function _id_autovalue() {
        return md5(uniqid('',true));
    }/**/
    function _id_check($x) {
        return ereg('^[a-zA-Z0-9]{32}$',$x);
    }
    
    /**
    * @var type desc
    */
    var $ip;
    function _ip_autovalue() {
        return $_SERVER['REMOTE_ADDR'];
    }/**/
    
    /**
    * @var type desc
    */
    var $uri;
    function _uri_autovalue() {
        return $_SERVER['REQUEST_URI'];
    }/**/
    
    /**
    * @var type desc
    */
    var $user_id;
    function _user_id_required() {
        return true;
    }
    function _user_id_fromdb($x) {
        return intval($x);
    }
    
    /**
    * @var type desc
    */
    var $started;
    function _started_autovalue() {
        return new datetimebean();
    }/**/
    function & _started_inputfilter($value) {
        if (!is_null($value) and !is_a($value,'datetimebean'))
            $value=&new datetimebean($value);
        return $value;
    }
    
    /**
    * @var type desc
    */
    var $limited;
    function _limited_autovalue() {
        return new datetimebean();
    }/**/
    function & _limited_inputfilter($value) {
        if (!is_null($value) and !is_a($value,'datetimebean'))
            $value=&new datetimebean($value);
        return $value;
    }
    
    /**
    * @var type desc
    */
    var $closed;
    function _closed_inputfilter($value) {
        if (!is_null($value) and !is_a($value,'datetimebean'))
            $value=&new datetimebean($value);
        return $value;
    }
    
/*------------------------------------------------------------------------------
                                                                 query templates
------------------------------------------------------------------------------*/
    /**
    * @var string query template
    */
    function _querytpl_save() {
        return 'REPLACE INTO user_auth_sessions SET
    id=$id
    {{, ip=$ip}}
    {{, uri=$uri}}
    , user_id=$user_id
    , started=$started
    , limited=$limited
    {{, closed=$closed}}';
    }
    /**
    * @var string query template
    */
    function _querytpl_load() {
        return 'SELECT *
    FROM user_auth_sessions
    WHERE id=$id';
    }
    /**
    * @var string query template
    */
    // var $_sqlrecordbean_query_{id}
/*------------------------------------------------------------------------------
                                                                   global config
------------------------------------------------------------------------------*/
    // var $_sqlrecordbean_primarykey='';
    
    //var $_sqlrecordbean_lastquery;
    
    // var $_sqlrecordbean_lastsqlerror;
    
/*------------------------------------------------------------------------------
                                                                     constructor
------------------------------------------------------------------------------*/
    /**
    *
    * @access
    * /
    function () {
        $this->__construct();
    }/**/
    
    /**
    * real constructor
    * @access
    * /
    function __construct() {
        
    }/**/
/*------------------------------------------------------------------------------
                                                                  public methods
------------------------------------------------------------------------------*/
    /**
    * sets session duration
    * @param int $duration
    * @access public
    * @return string
    */
    function setDuration($duration) {
        $this->limited=$this->started;
        return $this->limited->add($duration);
    }/**/
    
    /**
    * tells wether session is finished (closed or limit reached)
    * @access public
    * @return bool
    */
    function isFinished() {
        if ($this->closed)
            return true;
        if ($this->limited->isPast())
            return true;
        return false;
    }/**/
    
    /**
    * close session
    * @param
    * @access public
    * @return bool
    */
    function close() {
        $this->closed=datetimebean::now();
        return $this->save();
    }/**/
}
?>
